﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SHack.Helper
{
    class IPHelper
    {
      
        public static long IPToNumber(string ip) {
            long rs = 0;
            if (ip == null || ip=="") {
                return rs;
            }
            string[] ips = ip.Split('.');
            for (int i = 0; i < ips.Length; i++) {
                rs += (long)(Convert.ToInt32(ips[i]) * Math.Pow(256, (3 - i)));
            }
            return rs;
        }


        public static String NumberToIP(long number)
        {
            String ip = "";
            List<string> ips = new List<string>();
            for (int i = 0; i < 4; i++)
            {
                ips.Add(Convert.ToInt32((number % 256)).ToString());
                number = number >> 8;
            }
            for (int i = ips.Count - 1; i >= 0; i--)
            {
                ip += ips[i];
                if (i > 0)
                {
                    ip +=".";
                }
            }
            return ip;
        }
    }
}
